from panda3d.core import Vec3, VBase3


class WebTower(object):

    def __init__(self,  name, hp, range, atkspeed, damage, radius, cost, upgrades, worth, projectile, aggro):
        self.atkspeed = atkspeed
        self.range = range
        self.dmg = damage
        self.name = name
        self.upgrades = upgrades
        self.cost = cost
        self.amount = 1
        self.radius = radius
        self.pos = Vec3(0, 0, 0)
        self.manager = None
        self.curTarget = None
        self.hp = hp
        self.atktimer = 0
        self.worth = worth
        self.projectile = projectile
        self.aggro = aggro
        self.dead = False

    def update(self, dt, units):
        if self.curTarget is None or self.curTarget.dead:
            self.lock_on_targets(units)
        else:
            self.atktimer -= dt
            if self.atktimer <= 0:
                self.atktimer += self.atkspeed
                self.fire_at_target()

    def lock_on_targets(self, units):
        for u in units:
            dis = VBase3.length(u.pos - self.pos)
            if dis <= self.range:
                self.curTarget = u
                self.atktimer = 0

    def fire_at_target(self):
        self.manager.fire_projectile(self.projectile, self.curTarget, self.pos, self.dmg)
        return

    def damage(self, dmg):
        print "damage!"
        print dmg
        print self.hp
        if self.dead:
            return
        self.hp -= dmg
        print self.hp
        if self.hp <= 0:
            self.dead = True
            self.manager.destiny(self)

